﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Core;
using SsTool.Utils;

namespace SsTool.Core.SubPages
{
    /// <summary>
    /// Interaction logic for RoleNavigationPage.xaml
    /// </summary>
    public partial class RoleNavigationPage : Page
    {
        private SmartSpace.Core.Application _application;

        private List<TreeData<Navigation>> _navigations;

        private List<Navigation> _roleNavigations;

        public RoleNavigationPage(SmartSpace.Core.Application application)
        {
            InitializeComponent();

            _application = application;

            this.Loaded += new RoutedEventHandler(RoleNavigationPage_Loaded);
        }

        void RoleNavigationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadRoles();
        }

        private void LoadRoles()
        {
            ICoreService service = App.Get<ICoreService>();
            _ctlGridRole.ItemsSource = service.GetRoles(0, 999);
        }

        #region 加载导航
        private void LoadNavigations(Role selectedRole)
        {
            ICoreService service = App.Get<ICoreService>();
            List<Navigation> allNavigations = service.GetNavigations(_application.Id);
            _roleNavigations = service.GetNavigations(_application.Id, selectedRole.Id);

            _navigations = new List<TreeData<Navigation>>();
            foreach (var item in allNavigations)
            {
                _navigations.Add(ConvertNavigation(item));
            }

            _ctlTreeNavigation.ItemsSource = _navigations;
            _ctlTreeRoleNavigation.ItemsSource = _roleNavigations;
        }

        private TreeData<Navigation> ConvertNavigation(Navigation navigation)
        {
            TreeData<Navigation> data = new TreeData<Navigation>(navigation);
            data.IsSelected = IsSelected(navigation);

            foreach (Navigation item in navigation.Children)
            {
                data.Add(ConvertNavigation(item));
            }
            return data;
        }

        private bool IsSelected(Navigation navigation)
        {
            Stack<Navigation> stack = new Stack<Navigation>();
            foreach (var item in _roleNavigations)
            {
                stack.Push(item);    
            }

            while (stack.Count > 0)
            {
                Navigation item = stack.Pop();
                if (item.Id == navigation.Id)
                {
                    return true;
                }
                else
                {
                    foreach (var subItem in item.Children)
                    {
                        stack.Push(subItem);
                    }
                }
            }
            return false;
        }
        #endregion

        #region 加载快捷方式
        private void LoadShortcuts(Role selectedRole)
        {
            ICoreService service = App.Get<ICoreService>();
            _ctlGridShortcuts.ItemsSource = service.GetShortcuts(_application.Id, selectedRole.Id);
        }
        #endregion

        private void _ctlBtnSave_Click(object sender, RoutedEventArgs e)
        {
            Role selectedRole = _ctlGridRole.SelectedItem as Role;
            if (selectedRole != null)
            {
                List<Guid> selectedItems = new List<Guid>();
                Stack<TreeData<Navigation>> stack = new Stack<TreeData<Navigation>>();
                foreach (var item in _navigations)
                {
                    stack.Push(item);
                }

                while (stack.Count > 0)
                {
                    TreeData<Navigation> item = stack.Pop();
                    if (item.IsSelected)
                    {
                        selectedItems.Add(item.Data.Id);
                    }

                    foreach (var subItem in item.Children)
                    {
                        stack.Push(subItem);
                    }
                }

                ICoreService service = App.Get<ICoreService>();
                if (service.SetRoleNavigations(selectedItems, selectedRole.Id))
                {
                    MessageBox.Show("设置成功!");
                }
                else
                {
                    MessageBox.Show("设置失败!");
                }
            }
        }

        private void _ctlGridRole_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Role selectedRole = _ctlGridRole.SelectedItem as Role;
            if (selectedRole != null)
            {
                LoadNavigations(selectedRole);
                LoadShortcuts(selectedRole);
            }
        }
    }
}
